time_to_school <-
  read.csv("./_3_data/indonesia_time_to_school_data.csv") %>%
  mutate(label = case_when(str_detect(dl2type, "Elem") ~ "Elementary School",
                         str_detect(dl2type, "Junio") ~ "Middle School",
                         str_detect(dl2type, "Senio") ~ "High School",
                         str_detect(dl2type, "D1") ~ "University")) %>%
  mutate(label = factor(label, levels = c("Elementary School", "Middle School", "High School", "University"))) %>%
  ggplot(aes(x = log10(pop), y = min)) +
  geom_point(color="darkgrey", shape = 21, alpha = 0.5) + 
  facet_wrap(label ~ .) +
  geom_smooth(method = "lm", color = "black") +
  stat_summary_bin(fun.y='mean', bins=20,
                   shape = 21, fill = "lightgrey",
                   color='black', size=2.5, geom='point') +
  theme_bw() +
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(),
        axis.line.y.left = element_blank(),
        axis.title.y = element_blank()) +
  scale_colour_grey() +
  ylim(0, 60) +
  xlab("Population, log scale") +
  ggtitle("Distance to School (minutes), Indonesian Cities, By Population (logged, 2014)")

ggsave("./_4_outputs/figure_oa_iv.pdf", plot = time_to_school, width = 8, height = 5)
